package com.dg.cuckoo.admin.service.impl;

import com.dg.cuckoo.admin.dto.basicInformation.projectInformation.BasicProjectDto;
import com.dg.cuckoo.admin.dto.earlyProject.EarlyProjectDto;
import com.dg.cuckoo.admin.dto.proCarryOut.AppropriationDto;
import com.dg.cuckoo.admin.dto.proCarryOut.ProCarryOutDto;
import com.dg.cuckoo.admin.dto.proCarryOut.ProChangeDto;
import com.dg.cuckoo.admin.dto.proCarryOut.ProExpendDto;
import com.dg.cuckoo.admin.repository.*;
import com.dg.cuckoo.admin.service.ProjectReportService;
import com.dg.cuckoo.core.ServiceException;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class ProjectReportServiceimpl implements ProjectReportService {

    @Autowired
    private BasicProjectRepository basicProjectRepository;
    @Autowired
    private EarlyProjectRepository earlyProjectRepository;
    @Autowired
    private ProCarryOutRepository proCarryOutRepository;
    @Autowired
    private AppropriationRepository appropriationRepository;
    @Autowired
    private ProExpendRepository proExpendRepository;
    @Autowired
    private ProChangeRepository proChangeRepository;

    @Override
    public String selectById(String id) {
        if(id==null){
            throw new ServiceException("请选择项目");
        }
        JSONObject json = new JSONObject();
        BasicProjectDto projectDto = basicProjectRepository.findById(id).get();
        json.put("jbxx",projectDto);//基本信息

        List<EarlyProjectDto> earlyProjectDtos=new ArrayList<>();
        earlyProjectDtos.addAll(earlyProjectRepository.findByProjectIdAndType(id, 1));
        earlyProjectDtos.addAll(earlyProjectRepository.findByProjectIdAndType(id, 2));
        earlyProjectDtos.addAll(earlyProjectRepository.findByProjectIdAndType(id, 3));
        earlyProjectDtos.addAll(earlyProjectRepository.findByProjectIdAndType(id, 4));
        earlyProjectDtos.addAll(earlyProjectRepository.findByProjectIdAndType(id, 5));
        earlyProjectDtos.addAll(earlyProjectRepository.findByProjectIdAndType(id, 6));
        json.put("xmzbq",earlyProjectDtos);//项目准备期

        List<EarlyProjectDto> pppProjectDtos=new ArrayList<>();
        pppProjectDtos.addAll(earlyProjectRepository.findByProjectIdAndType(id, 7));
        pppProjectDtos.addAll(earlyProjectRepository.findByProjectIdAndType(id, 8));
        pppProjectDtos.addAll(earlyProjectRepository.findByProjectIdAndType(id, 9));
        json.put("ppp",pppProjectDtos);//ppp项目

        List<EarlyProjectDto> kxxProjectDtos=new ArrayList<>();
        kxxProjectDtos.addAll(earlyProjectRepository.findByProjectIdAndType(id, 10));
        kxxProjectDtos.addAll(earlyProjectRepository.findByProjectIdAndType(id, 11));
        json.put("kxxyj",kxxProjectDtos);//可行性研究

        List<EarlyProjectDto> cbProjectDtos=new ArrayList<>();
        cbProjectDtos.addAll(earlyProjectRepository.findByProjectIdAndType(id, 12));
        cbProjectDtos.addAll(earlyProjectRepository.findByProjectIdAndType(id, 13));
        json.put("cbsj",cbProjectDtos);//初步设计

        List<EarlyProjectDto> sgProjectDtos=new ArrayList<>();
        sgProjectDtos.addAll(earlyProjectRepository.findByProjectIdAndType(id, 14));
        sgProjectDtos.addAll(earlyProjectRepository.findByProjectIdAndType(id, 15));
        json.put("sgt",sgProjectDtos);//施工图

        List<EarlyProjectDto> otherProjectDtos=new ArrayList<>();
        otherProjectDtos.addAll(earlyProjectRepository.findByProjectIdAndType(id, 16));
        otherProjectDtos.addAll(earlyProjectRepository.findByProjectIdAndType(id, 17));
        otherProjectDtos.addAll(earlyProjectRepository.findByProjectIdAndType(id, 18));
        otherProjectDtos.addAll(earlyProjectRepository.findByProjectIdAndType(id, 19));
        json.put("qt",otherProjectDtos);//其他

        List<ProCarryOutDto> htProjectDtos=new ArrayList<>();
        htProjectDtos.addAll(proCarryOutRepository.findByProjectIdAndType(id, 1));
        htProjectDtos.addAll(proCarryOutRepository.findByProjectIdAndType(id, 2));
        htProjectDtos.addAll(proCarryOutRepository.findByProjectIdAndType(id, 3));
        htProjectDtos.addAll(proCarryOutRepository.findByProjectIdAndType(id, 4));
        htProjectDtos.addAll(proCarryOutRepository.findByProjectIdAndType(id, 5));
        htProjectDtos.addAll(proCarryOutRepository.findByProjectIdAndType(id, 6));
        htProjectDtos.addAll(proCarryOutRepository.findByProjectIdAndType(id, 7));
        htProjectDtos.addAll(proCarryOutRepository.findByProjectIdAndType(id, 8));
        json.put("xmht",htProjectDtos);//项目合同

        List<AppropriationDto> zjbfProjectDtos=new ArrayList<>();
        zjbfProjectDtos.addAll(appropriationRepository.findByProjectIdAndType(id,1));
        zjbfProjectDtos.addAll(appropriationRepository.findByProjectIdAndType(id,2));
        zjbfProjectDtos.addAll(appropriationRepository.findByProjectIdAndType(id,3));
        zjbfProjectDtos.addAll(appropriationRepository.findByProjectIdAndType(id,4));
        json.put("zjbf",zjbfProjectDtos);//资金拨付

        List<ProExpendDto> xmzcProjectDtos=new ArrayList<>();
        xmzcProjectDtos.addAll(proExpendRepository.findByProjectIdAndType(id,1));
        xmzcProjectDtos.addAll(proExpendRepository.findByProjectIdAndType(id,2));
        xmzcProjectDtos.addAll(proExpendRepository.findByProjectIdAndType(id,3));
        xmzcProjectDtos.addAll(proExpendRepository.findByProjectIdAndType(id,4));
        xmzcProjectDtos.addAll(proExpendRepository.findByProjectIdAndType(id,5));
        xmzcProjectDtos.addAll(proExpendRepository.findByProjectIdAndType(id,6));
        xmzcProjectDtos.addAll(proExpendRepository.findByProjectIdAndType(id,7));
        xmzcProjectDtos.addAll(proExpendRepository.findByProjectIdAndType(id,8));
        json.put("xmzc",xmzcProjectDtos);//项目支出

        List<ProChangeDto> bgProjectDtos=new ArrayList<>();
        bgProjectDtos.addAll(proChangeRepository.findByProjectIdAndChangeType(id,1));
        json.put("xmbg",bgProjectDtos);//项目变更
        return json.toString();
    }
}
